package com.globalblue.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import com.globalblue.FileListDisplay;
import java.awt.FlowLayout;

public class FileSelectorWindow {

	private JFrame frame;
	private JPanel panel1;
	private JPanel fileListPanel;
	private JPanel leftCheckBoxPanel;
	private JPanel rightCheckBoxPanel;
	private JSplitPane splitPane;
	private JScrollPane leftFileList;
	private JScrollPane rightFileList;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FileSelectorWindow window = new FileSelectorWindow();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public FileSelectorWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setResizable(false);
		frame.setBounds(100, 100, 820, 575);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		panel1 = new JPanel();
		panel1.setBounds(0, 0, 814, 557);
		panel1.setPreferredSize(new Dimension(300, 280));
		panel1.setBorder(new LineBorder(new Color(0, 0, 0)));
		frame.getContentPane().setLayout(null);
		frame.getContentPane().add(panel1);
		panel1.setLayout(null);
		
		JSplitPane splitPane_1 = new JSplitPane();
		splitPane_1.setBounds(22, 110, 782, 227);
		splitPane_1.setDividerLocation(0.5);
		panel1.add(splitPane_1);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		splitPane_1.setRightComponent(scrollPane_1);
		
		JPanel panel_1 = new JPanel();
		scrollPane_1.setColumnHeaderView(panel_1);
		panel_1.setLayout(null);
		panel_1.setPreferredSize(new Dimension(100,222));
		
		JScrollPane scrollPane = new JScrollPane();
		
		splitPane_1.setLeftComponent(scrollPane);
		
		JPanel panel = new JPanel();
		scrollPane.setViewportView(panel);
		panel.setLayout(null);
		panel.setPreferredSize(new Dimension(100,222));
		
		JButton btnDeploy = new JButton("Deploy");
		btnDeploy.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnDeploy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnDeploy.setBounds(358, 370, 89, 23);
		panel1.add(btnDeploy);
		
		JLabel lblSelectFilesAnd = new JLabel("Select files and folders to be deployed");
		lblSelectFilesAnd.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblSelectFilesAnd.setBounds(254, 43, 374, 28);
		panel1.add(lblSelectFilesAnd);

	}

	public void showMainWindow() {
		frame.setVisible(true);
	}

	

	public void showFileListWindow(final List<String> fileList) {

		try {
			fileListPanel = new JPanel();
			
			leftFileList=new JScrollPane();
			rightFileList=new JScrollPane();
			leftFileList.setMinimumSize(new Dimension(30,30));
			rightFileList.setMinimumSize(new Dimension(30,30));
			leftFileList.setBackground(new Color(8126463));
			leftFileList.setBounds(0, 0, 814, 280);
			leftFileList.setLayout(null);
			rightFileList.setLayout(null);

			fileListPanel.setLayout(null);
			
			JButton btnNewButton = new JButton("Deploy");
			btnNewButton.setBounds(326, 234, 89, 23);
			fileListPanel.add(btnNewButton);
			leftCheckBoxPanel = new JPanel();
			leftCheckBoxPanel.setBounds(0, 0, 814, 280);
			leftCheckBoxPanel.setLayout(null);
			rightCheckBoxPanel = new JPanel();
			rightCheckBoxPanel.setBounds(10, 11, 414, 306);
			rightCheckBoxPanel.setLayout(null);

			int position = 17;
			JCheckBox[] checkBoxList = new JCheckBox[fileList.size()];
			for (int i = 0; i < fileList.size(); i++) {
				String fileName = fileList.get(i);
				checkBoxList[i] = new JCheckBox(fileName);
			}

			for (int i = 0; i < fileList.size() / 2; i++) {
				JCheckBox checkBox = checkBoxList[i];
				checkBox.setBounds(6, position, 400, 23);
				position += 20;
				leftCheckBoxPanel.add(checkBox);
			}

			for (int i = fileList.size() / 2; i < fileList.size(); i++) {
				JCheckBox checkBox = checkBoxList[i];
				checkBox.setBounds(6, position, 400, 23);
				position += 20;
				rightCheckBoxPanel.add(checkBox);
			}
			leftFileList.add(leftCheckBoxPanel);
			rightFileList.add(rightCheckBoxPanel);
			
			splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
					leftFileList, rightFileList);
			splitPane.setOneTouchExpandable(true);
			splitPane.setDividerLocation(150);
			fileListPanel.add(splitPane);
			frame.getContentPane().remove(panel1);
			frame.revalidate();
			fileListPanel.setBounds(0, 0, 814, 280);
			fileListPanel.setPreferredSize(new Dimension(300, 280));
			fileListPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
			frame.getContentPane().add(fileListPanel);
			frame.revalidate();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
